/**
 * 执行用时为 0 ms 的范例
 */

#include <stdio.h>
#include <stdlib.h>
// 这里应该使用 .h 的文件的，但是因为 `Code runner`会编译不过去，就用 .c 。
// 因为我们仅有一个入口文件调用，可用使用 .c ，知道就可以了，我是为了学习思路的。
#include "../../utils/c/c_tool.c"
// #include "../utils/c/c_tool.h"

struct ListNode
{
  int val;
  struct ListNode *next;
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{
  struct ListNode *pl1 = l1, *pl2 = l2;
  int num1[1000] = {0}, num2[1000] = {0}, sum[1000], i, j, k;
  for (i = 0; pl1; i++)
  {
    num1[i] = pl1->val;
    pl1 = pl1->next;
  }

  for (j = 0; pl2; j++)
  {
    num2[j] = pl2->val;
    pl2 = pl2->next;
  }

  int temp = 0;
  if (i > j)
  {
    for (k = 0; k < i; k++)
    {
      sum[k] = (num1[k] + num2[k] + temp) % 10;
      temp = (num1[k] + num2[k] + temp) / 10;
    }
  }
  else
  {
    for (k = 0; k < j; k++)
    {
      sum[k] = (num1[k] + num2[k] + temp) % 10;
      temp = (num1[k] + num2[k] + temp) / 10;
    }
  }

  // printf("temp=%d\n",temp);
  if (temp != 0)
  {
    sum[k] = temp;
    k++;
  }
  //printf("%d\n",temp);

  struct ListNode *head = NULL, *tail = NULL;
  i = 0;
  while (i < k)
  {
    struct ListNode *l3 = (struct ListNode *)malloc(sizeof(struct ListNode));
    if (head == NULL)
    {
      head = l3;
      l3->next = NULL;
    }
    else
    {
      tail->next = l3;
      l3->next = NULL;
    }
    l3->val = sum[i];
    tail = l3;
    i++;
  }
  return head;
}





struct ListNode * create_node(int *list, int listSize) {
  if (list == NULL) return NULL;
  struct ListNode *resultNodel = NULL;
  int length = listSize - 1;
  for (int i = 0; i < listSize; i++) {
    struct ListNode *currListNode = (struct ListNode *)malloc(sizeof(struct ListNode));
    currListNode->val = list[length - i];
    if (i == 0) {
      currListNode->next = NULL;
    } else {
      currListNode->next = resultNodel;
    }
    resultNodel = currListNode;
  }
  return resultNodel;
}

void printStructList(struct ListNode *node) {
  if (!node) {
    printf("no valid value");
    return;
  }

  printf("\n结果：\n[");
  int start = 1;
  while (node)
  {
    if (start == 0) {
      printf(", ");
    }
    printf("%d", node->val);
    start = 0;
    node = node->next;
  }

  printf("]\n");
}

int main() {
  // int l1_list[] = {2,4,3};
  // int l2_list[] = {5,6,4};

  // int l1_list[] = {0};
  // int l2_list[] = {0};

  // int l1_list[] = {9,9,9,9,9,9,9};
  // int l2_list[] = {9,9,9,9};

  int l1_list[] = {9,9,9,9,9,9,9};
  int l2_list[] = {9,9,8};


  
  int *ttL1 = l1_list;
  int ttl1Len = sizeof(l1_list) / sizeof(l1_list[0]);
  printf("\n11 ttl1Len=%d", ttl1Len);
  int *ttL2 = l2_list;
  int ttl2Len = sizeof(l2_list) / sizeof(l2_list[0]);
  printf("\n22 ttl2Len=%d", ttl2Len);

  ToolPrintArray(ttL1, ttl1Len);
  ToolPrintArray(ttL2, ttl2Len);

  struct ListNode *l1 = create_node(ttL1, ttl1Len);
  struct ListNode *l2 = create_node(ttL2, ttl2Len);
  // printStructList(l1);
  // printStructList(l2);

  struct ListNode * resultNode = addTwoNumbers(l1, l2);
  printStructList(resultNode);
}